package extreme.refactor.gilded_rose;

class GildedRose {
    public static final String BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT = "Backstage passes to a TAFKAL80ETC concert";
    public static final String SULFURAS_HAND_OF_RAGNAROS = "Sulfuras, Hand of Ragnaros";
    public static final String AGED_BRIE = "Aged Brie";
    Item[] items;

    public GildedRose(Item[] items) {
        this.items = items;
    }

    public void update_quality() {
        for (int i = 0; i < items.length; i++) {

            if (isEquals(items[i], AGED_BRIE)
                    && !isEquals(items[i], BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT)) {
                priceMinusOne(i);
                continue;
            }

            if (items[i].quality < 50) {
                items[i].quality = items[i].quality + 1;

                if (isEquals(items[i], BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT)) {
                    if (items[i].sell_in < 11) {
                        pricePlusOne(i);
                    }

                    if (items[i].sell_in < 6) {
                        pricePlusOne(i);
                    }
                }
            }

            if (!isEquals(items[i], SULFURAS_HAND_OF_RAGNAROS)) {
                items[i].sell_in = items[i].sell_in - 1;
            }

            if (items[i].sell_in >= 0) {
                continue;
            }

            if (isEquals(items[i], AGED_BRIE)) {
                pricePlusOne(i);
                continue;
            }
            if (isEquals(items[i], BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT)) {
                items[i].quality = 0;
                continue;
            }
            priceMinusOne(i);
        }
    }

    private boolean isEquals(Item item, String s) {
        return item.name.equals(s);
    }


    private void pricePlusOne(int i) {
        if (items[i].quality < 50) {
            items[i].quality = items[i].quality + 1;
        }
    }

    private void priceMinusOne(int i) {
        if (items[i].quality > 0) {
            if (!isEquals(items[i], SULFURAS_HAND_OF_RAGNAROS)) {
                items[i].quality = items[i].quality - 1;
            }
        }
    }
}
